home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xlog.1.0.9 / xlog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-07  |  4.7 KB  |  281 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: xlog.c,v 1.4 1992/11/24 20:49:23 panos Exp $" ;
  8. static char version[] = VERSION ;
  9.  
  10. #include <varargs.h>
  11.  
  12. #include "xlog.h"
  13. #include "impl.h"
  14.  
  15. char *malloc() ;
  16.  
  17. extern struct xlog_ops __xlog_filelog_ops ;
  18. #ifndef NO_SYSLOG
  19. extern struct xlog_ops __xlog_syslog_ops ;
  20. #endif
  21.  
  22. struct lookup_table
  23. {
  24.     struct xlog_ops *ops ;
  25.     xlog_e type ;
  26. } ;
  27.  
  28. static struct lookup_table ops_lookup_table[] =
  29.     {
  30.         { &__xlog_filelog_ops,                XLOG_FILELOG    },
  31. #ifndef NO_SYSLOG
  32.         { &__xlog_syslog_ops,                XLOG_SYSLOG        },
  33. #endif
  34.         { NULL }
  35.     } ;
  36.  
  37.  
  38. #define CALLBACK( xp, status )                                                                 \
  39.                 if ( (xp)->callback )                                                             \
  40.                     (*(xp)->callback)( (xlog_h)(xp), status, (xp)->callback_arg )
  41.  
  42.  
  43. PRIVATE void xlog_link() ;
  44. PRIVATE void xlog_unlink() ;
  45. PRIVATE struct xlog_ops *xlog_ops_lookup() ;
  46.  
  47.  
  48. /* VARARGS3 */
  49. xlog_h xlog_create( type, id, flags, va_alist )
  50.     xlog_e type ;
  51.     char *id ;
  52.     int flags ;
  53.     va_dcl
  54. {
  55.     xlog_s *xp ;
  56.     va_list ap ;
  57.     struct xlog_ops *xops ;
  58.     int status ;
  59.  
  60.     if ( ( xp = NEW( xlog_s ) ) == NULL )
  61.         return( NULL ) ;
  62.     
  63.     if ( id == NULL || ( xp->id = __xlog_new_string( id ) ) == NULL )
  64.     {
  65.         FREE( xp ) ;
  66.         return( NULL ) ;
  67.     }
  68.  
  69.     xops = xlog_ops_lookup( type ) ;
  70.     
  71.     if ( xops != NULL )
  72.     {
  73.         va_start( ap ) ;
  74.         status = (*xops->init)( xp, ap ) ;
  75.         va_end( ap ) ;
  76.  
  77.         if ( status == XLOG_ENOERROR )
  78.         {
  79.             xp->active = TRUE ;
  80.             xp->flags = flags ;
  81.             xp->type = type ;
  82.             xp->clients = XLOG_NULL ;
  83.             xp->use = XLOG_NULL ;
  84.             xp->ops = xops ;
  85.             return( (xlog_h) xp ) ;
  86.         }
  87.     }
  88.  
  89.     free( xp->id ) ;
  90.     FREE( xp ) ;
  91.     return( NULL ) ;
  92. }
  93.  
  94.  
  95. PRIVATE struct xlog_ops *xlog_ops_lookup( type )
  96.     register xlog_e type ;
  97. {
  98.     register struct lookup_table *ltp ;
  99.  
  100.     for ( ltp = &ops_lookup_table[ 0 ] ; ltp->ops ; ltp++ )
  101.         if ( ltp->type == type )
  102.             break ;
  103.     return( ltp->ops ) ;
  104. }
  105.  
  106.  
  107.  
  108. PRIVATE void xlog_link( client, server )
  109.     xlog_s *client, *server ;
  110. {
  111.     client->use = server ;
  112.     if ( server == NULL )
  113.         return ;
  114.  
  115.     if ( server->clients == XLOG_NULL )
  116.     {
  117.         INIT_LINKS( client, other_users ) ;
  118.         server->clients = client ;
  119.     }
  120.     else
  121.         LINK( server, client, other_users ) ;
  122. }
  123.  
  124.  
  125. PRIVATE void xlog_unlink( xp )
  126.     xlog_s *xp ;
  127. {
  128.     xlog_s *server = xp->use ;
  129.  
  130.     /*
  131.      * Step 1: remove from server chain
  132.      */
  133.     if ( server != XLOG_NULL )
  134.     {
  135.         if ( server->clients == xp )
  136.             if ( NEXT( xp, other_users ) == xp )
  137.                 server->clients = XLOG_NULL ;
  138.             else
  139.                 server->clients = NEXT( xp, other_users ) ;
  140.         else
  141.             UNLINK( xp, other_users ) ;
  142.     }
  143.  
  144.     /*
  145.      * Step 2: If we have users, clear their link to us.
  146.      */
  147.     if ( xp->clients != NULL )
  148.     {
  149.         xlog_s *xp2 = xp->clients ;
  150.  
  151.         do
  152.         {
  153.             xp2->use = XLOG_NULL ;
  154.             xp2 = NEXT( xp2, other_users ) ;
  155.         }
  156.         while ( xp2 != xp->clients ) ;
  157.     }
  158. }
  159.  
  160.  
  161. PRIVATE void xlog_flags( xp, cmd, ap )
  162.     xlog_s *xp ;
  163.     xlog_cmd_e cmd ;
  164.     va_list ap ;
  165. {
  166.     int flag = va_arg( ap, int ) ;
  167.     int old_value = ( ( xp->flags & flag ) != 0 ) ;
  168.     int *valp = va_arg( ap, int * ) ;
  169.  
  170.     if ( cmd == XLOG_SETFLAG )
  171.         if ( *valp )
  172.             xp->flags |= flag ;
  173.         else
  174.             xp->flags &= ~flag ;
  175.         *valp = old_value ;
  176. }
  177.  
  178.  
  179. void xlog_destroy( xlog )
  180.     xlog_h xlog ;
  181. {
  182.     xlog_s *xp = XP( xlog ) ;
  183.  
  184.     xlog_unlink( xp ) ;
  185.     (*xp->ops->fini)( xp ) ;
  186.     free( xp->id ) ;
  187.     FREE( xp ) ;
  188. }
  189.  
  190.  
  191. /* VARARGS4 */
  192. void xlog_write( xlog, buf, len, flags, va_alist )
  193.     xlog_h xlog ;
  194.     char buf[] ;
  195.     int len ;
  196.     int flags ;
  197.     va_dcl
  198. {
  199.     xlog_s *xp = XP( xlog ) ;
  200.     va_list ap ;
  201.     int status ;
  202.  
  203.     if ( ! xp->active )
  204.         return ;
  205.  
  206.     va_start( ap ) ;
  207.     status = (*xp->ops->write)( xp, buf, len, flags, ap ) ;
  208.     va_end( ap ) ;
  209.  
  210.     if ( status != XLOG_ENOERROR )
  211.     {
  212.         xp->active = FALSE ;
  213.         CALLBACK( xp, status ) ;
  214.     }
  215. }
  216.  
  217.  
  218. /* VARARGS2 */
  219. void xlog_control( xlog, cmd, va_alist )
  220.     xlog_h xlog ;
  221.     xlog_cmd_e cmd ;
  222.     va_dcl
  223. {
  224.     xlog_s *xp = XP( xlog ) ;
  225.     va_list ap ;
  226.     int status ;
  227.  
  228.     va_start( ap ) ;
  229.  
  230.     switch ( cmd )
  231.     {
  232.         case XLOG_LINK:
  233.             xlog_unlink( xp ) ;
  234.             xlog_link( xp, va_arg( ap, xlog_s * ) ) ;
  235.             xp->callback_arg = va_arg( ap, void * ) ;
  236.             break ;
  237.         
  238.         case XLOG_CALLBACK:
  239.             xp->callback = va_arg( ap, voidfunc ) ;
  240.             break ;
  241.             
  242.         case XLOG_GETFLAG:
  243.         case XLOG_SETFLAG:
  244.             xlog_flags( xp, cmd, ap ) ;
  245.             status = XLOG_ENOERROR ;
  246.             break ;
  247.  
  248.         default:
  249.             status = (*xp->ops->control)( xp, cmd, ap ) ;
  250.             if ( status == XLOG_ENOERROR )
  251.                 xp->active = TRUE ;
  252.             else
  253.             {
  254.                 xp->active = FALSE ;
  255.                 CALLBACK( xp, status ) ;
  256.             }
  257.     }
  258.  
  259.     va_end( ap ) ;
  260. }
  261.  
  262.  
  263. int xlog_parms( type, va_alist )
  264.     xlog_e type ;
  265.     va_dcl
  266. {
  267.     va_list ap ;
  268.     int status ;
  269.  
  270.     va_start( ap ) ;
  271.     if ( type == XLOG_SYSLOG )
  272.         status = (*__xlog_syslog_ops.parms)( ap ) ;
  273.     else if ( type == XLOG_FILELOG )
  274.         status = (*__xlog_filelog_ops.parms)( ap ) ;
  275.     else
  276.         status = XLOG_ENOERROR ;
  277.     va_end( ap ) ;
  278.     return( status ) ;
  279. }
  280.  
  281.